iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 25
1
Software Development

PHP新手30天實戰金流系列 第 25

[Day25]平台串金流--PAYPAL migrating V1 to V2

  • 分享至 

  • xImage
  •  
PHP新手30天實戰金流, Laravel6

前言

官網提及 Please note that if you are integrating with PayPal Checkout, this SDK and corresponding API v1/payments are in the process of being deprecated.因此我們決定要做版本更新,
官網有一份文件 Migrating from v1 to v2 供參考,以下程式碼皆出自於此。

Version 2

"2.0 includes all of the existing APIs (except payouts), and includes the new Orders API (Disputes and Marketplace coming soon)."

"We've simplified the interface to only provide HTTPRequest that can easily be called via our HttpClient."

How to update?

修改 composer.jsonpaypal/rest-api-sdk-php 的版本為 dev-2.0-beta 然後 composer update

What's diff?

先簡介付款流程:

  1. create a payment
  2. authorize the payment by buyer
  3. execute the authorized payment
  4. Merchant capture the authorized payment, done
    接著,買家可以提出退款申請:
  5. refund the capture

更新到 version 2,要修改的地方為付款的第一步驟:

  1. 原本用 apicontext 來記錄商家(client)資訊,現在改用 SandboxEnvironment 和 PayPalHttpClient 類別來處理。

    原本:

    $apiContext = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential(
            'client-id',
            'client-secret'
        )
    );
    
    $apiContext->setConfig(
        array('mode' => 'live')
    );
    

    現在:

    use PayPal\Core\PayPalHttpClient;
    use PayPal\Core\SandboxEnvironment;
    
    $environment = new SandboxEnvironment('client-id', 'client-secret'); // Use `ProductionEnvironment` for production
    $client = new PayPalHttpClient($environment);
    
  2. 原本的body用物件串起, 現在直接寫在 body上:
    原本:

    $payer = new \PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');
    
    $amount = new \PayPal\Api\Amount();
    $amount->setTotal('1.00');
    $amount->setCurrency('USD');
    
    $transaction = new \PayPal\Api\Transaction();
    $transaction->setAmount($amount);
    
    $redirectUrls = new \PayPal\Api\RedirectUrls();
    $redirectUrls->setReturnUrl("https://example.com/your_redirect_url.html")
        ->setCancelUrl("https://example.com/your_cancel_url.html");
    
    $payment = new \PayPal\Api\Payment();
    $payment->setIntent('sale')
        ->setPayer($payer)
        ->setTransactions(array($transaction))
        ->setRedirectUrls($redirectUrls);
    
    try {
        $payment->create($apiContext);
        echo $payment;
    }
    catch (\PayPal\Exception\PayPalConnectionException $ex) {
        echo $ex->getData();
    }
    

    現在:

    $body = [
        "intent" => "sale",
        "transactions" => [
            [
                "amount" => [
                    "total" => "10",
                    "currency" => "USD"
                ]
            ]
        ],
        "redirect_urls" => [
            "cancel_url" => "http://paypal.com/cancel",
            "return_url" => "http://paypal.com/return"
        ],
        "payer" => [
            "payment_method" => "paypal"
        ]
    ];
    
    $request = new PaymentCreateRequest();
    $request->body = $body;
    
    try {
        return $client->execute($request);
    } catch (HttpException $ex) {
        echo $ex->statusCode;
        print_r($ex->getMessage());
    }
    

    會得到

        {
        "statusCode": 201,
        "result": {
            "id": "PAYID-LWQZ4SQ78W31241DC6035226",
            "intent": "sale",
            "state": "created",
            "payer": {
                "payment_method": "paypal"
            },
            "transactions": [
                {
                    "amount": {
                        "total": "470.00",
                        "currency": "THB"
                    },
                    "related_resources": []
                }
            ],
            "create_time": "2019-10-12T09:35:05Z",
            "links": [
                {
                    "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LWQZ4SQ78W31241DC6035226",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-8FE75461FU181391W",
                    "rel": "approval_url",
                    "method": "REDIRECT"
                },
                {
                    "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LWQZ4SQ78W31241DC6035226/execute",
                    "rel": "execute",
                    "method": "POST"
                }
            ]
        },
        "headers": {
            "": "",
            "Date": "Sat, 12 Oct 2019 09",
            "Server": "Apache",
            "paypal-debug-id": "4e44cce1a690b",
            "Content-Language": "*",
            "HTTP_X_PP_AZ_LOCATOR": "sandbox.slc",
            "Paypal-Debug-Id": "4e44cce1a690b",
            "Set-Cookie": "X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00",
            "Vary": "Authorization",
            "Content-Length": "644",
            "Connection": "close",
            "Content-Type": "application/json"
        }
    }
    

上一篇
[Day24]平台串金流--PAYPAL 買家付款模式(order)
下一篇
[Day26]平台部署 GCP, Apache, Laravel
系列文
PHP新手30天實戰金流34
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言